package com.example.day1203_newsport.Dialog;

import com.example.day1203_newsport.ResourceTable;
import com.example.day1203_newsport.proveder.DeviceProvider;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.distributedschedule.interwork.DeviceInfo;

import java.util.List;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class PopupWindow extends CommonDialog {

    private final Context mContext;
    private final List<DeviceInfo> mDeviceInfo;
    // 回调
    private PopupWindowCallback mPopupWindowCallback;

    public PopupWindow(Context context, List<DeviceInfo> deviceInfoList) {

        super(context);
        this.mContext = context;
        this.mDeviceInfo = deviceInfoList;
    }


    @Override
    protected void onCreate() {
        Component component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_layout_devices_dialog, null, false);

        //设置对话框大小
        setSize(MATCH_PARENT, MATCH_CONTENT);
        //设置对齐属性
        setAlignment(LayoutAlignment.BOTTOM);
        //是否启用：点击对话框外部后，关闭对话框
        setAutoClosable(true);
        //透明
        setTransparent(true);
        //关联布局
        setContentCustomComponent(component);

        ListContainer listContainer = component.findComponentById(ResourceTable.Id_list_container);
        DeviceProvider itemProvider = new DeviceProvider(mDeviceInfo);
        listContainer.setItemProvider(itemProvider);

        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                if (mPopupWindowCallback !=null) {
                    DeviceInfo deviceInfo = mDeviceInfo.get(i);
                    mPopupWindowCallback.result(deviceInfo);
                }
            }
        });


    }

    public void setPopupWindowCallback(PopupWindowCallback popupWindowCallback) {

        this.mPopupWindowCallback = popupWindowCallback;
    }

    public interface PopupWindowCallback {
        void result(DeviceInfo deviceInfo1);
    }


}
